package org.helgoboss.mintty

import org.helgoboss.scala_additions.CurrentPlatform
import org.helgoboss.scala_additions.OperatingSystemClass._
import org.helgoboss.osgi_additions._
import org.helgoboss.osgi_additions.metatype._
import org.helgoboss.terminal.Terminal
import org.osgi.service.cm.ConfigurationAdmin
import java.io.File

class MinttyActivator extends OsgiContext with OsgiProvider with OsgiConfigurationWatcher with OsgiMetaTypeProvider {
    whenBundleActive {        
        provideMetaType(objectClass)
        
        whenConfigurationUpdated(objectClass.id) { conf =>
            val resultConf = conf match {
                case Some(c) => objectClass.defaultConfig ++ c
                case None => objectClass.defaultConfig
            }
            val exeFile = new File(resultConf("exeFile").asInstanceOf[String])
            val configFile = resultConf.get("configFile").map(_.asInstanceOf[String]).map(new File(_))
            new Mintty(exeFile = exeFile, configFile = configFile).providesService[Terminal]
        }
    }
    
    lazy val objectClass = ObjectClass (
        id = "org.helgoboss.mintty",
        name = "MinTTY",
        requiredAttributes = List (
            ElementaryAttribute[String](id = "exeFile", name = "MinTTY executable path", default = Some(defaultExeFile))
        ),
        optionalAttributes = List (
            ElementaryAttribute[String](id = "configFile", name = "MinTTY configuration file")
        )
    )
    
    val defaultExeFile = """C:\cygwin\bin\mintty.exe"""
}
